<!doctype html>
<html lang = "en">
<head>
    <title>Checkout</title>
    <link type="text/css" rel="stylesheet" href="../stylesheet.css"/>
</head>
<body>
	<h1 class="title">Sandwiches Elliphino's</h1>
	<div>
                <form method="" action="">
                    <input value ="Ayuda" type="submit" >
                </form>    
    </div>
    <?php
        require('esp_orderItem.php'); //include class file
        session_start(); //start session to get session variables
    ?>
    <h2 class="subtitle">Resumen de la Orden</h2>
	<div style="background-color: grey; float: left; margin-left: 15%; width: 350px; height: 500px; overflow: auto; border-style: solid; border-width: 5px">
    <p>
    <?php
        if(isset($_SESSION['orders']))
        {
            $username = 'root';
					$pw = 'password';
					$con = mysqli_connect ("localhost", $username, $pw, 'elliphinos');
		
					if (mysqli_connect_errno()) 
					{
						echo "Failed to connect to MySQL: " . mysqli_connect_error();
					}
		
					//set time to the central time zone
					if ( !date_default_timezone_set ( 'America/Chicago' ) )
					{
						echo "could not set time zone!";
					}
					
					//start foreach loop
			foreach($_SESSION['orders'] as $anOrder) //print information of each submitted order item
            {
                echo "<h3>" . $anOrder->sandwichType . "</h3>" . "</br>";
                echo "Pan: " . $anOrder->bread . "</br>";
                echo "Carne: " . $anOrder->meat . "</br>";
                echo "Queso: " . $anOrder->cheese . "</br>";
                echo "Verduras: " . $anOrder->veggies . "</br>";
                echo "Condimentos: " . $anOrder->condiments . "</br>";
                echo "Bebida: " . $anOrder->drink . "</br>";
                echo "Papas Fritas: " . $anOrder->chips . "</br>";
                echo "Postre: " . $anOrder->dessert . "</br>";
				$cost = sprintf ("%.2f", $anOrder->total);
                echo "Costo: $" . $cost . "</br>";
            }
        }
    ?>
    </p>
    <p>
    <?php
        $tax = sprintf("%.2f",floor(($_SESSION['orderTotal']*(.08))*100)/100); //round down to only 2 decimal places
		$total = sprintf ("%.2f",($_SESSION['orderTotal'] + $tax));
		$sub_total = sprintf ("%.2f", $_SESSION['orderTotal']);
        echo "Sub-total: $" . $sub_total . "</br>"; //displays sub-total (pre-tax)
        echo "Impuestos: $" . $tax . "</br>"; //displays rounded tax
        echo "Total: $" . $total . "</br>"; //displays total with tax included
    ?>
    </p>
	</div>
		<div>
        <form method = "link" action = "esp_accordionMenu.php">
                <input type = "submit" value = "Atras" style="font-size: 40px; position: absolute; top: 5%; left: 0%; width: 4em; height: 1.2em;">
            </form>
		</div>
		<div style="float: right; margin-right: 5%; width: 600px">
            <span style="font-size:15pt">
            <?php
                echo "Sub-total: $" . $sub_total . "</br>"; //displays sub-total (pre-tax)
                echo "Impuestos: $" . $tax . "</br>"; //displays rounded tax
                echo "Total: $" . $total . "</br>"; //displays total with tax included       
            ?>
            </span>
            <form method = "POST" action = "">
                <input type = "submit" name = "cash" value = "Pagar en Efectivo">
                <br>
               
         
        <?php
            if(isset($_POST['cash']))
            {
                
        ?>
                

            <br>
            <span style="font-size:15pt"> Ingrese el numero de efectivo con el que desea pagar: <br> $
            <input type="text" name="cashEntered" maxlength = "2" size="2" style="text-align:right;">
            <strong>.</strong>
			
                <input type = "text" name = "changeEntered" maxlength = "2" size="2" style="text-align:right;">
                </span>
                <br>
				<input type="submit" name = "button2" value="Enviar orden a la cocina">
              </form>
        <?php
            }

            if(isset($_POST['button2']))
            {
                
                //needs form validation 
                //Check if cash entered and change entered are numerical
                $cash = $_POST["cashEntered"];
                $moneyReturned = 0;
                
                $change = .01 * $_POST["changeEntered"];
                $amountEntered = $cash + $change;
                if($amountEntered >= $total)
                {
                    //echo "you payed enough";
                    $moneyReturned = $amountEntered - $total; 
                    $_SESSION['moneyReturned'] = $moneyReturned;
					
							$subtotal = $_SESSION['orderTotal'];
							$tax = floor(($subtotal*(.08))*100)/100;
							$total = $tax + $subtotal;
							$randomOrderId = rand(1,9999);
							echo $randomOrderId . "</br>";
							//update sales table with data about sandwiches
							$sql = "INSERT INTO sales (`order_id`, `subtotal`, `tax`, `total`) VALUES ($randomOrderId, $subtotal, $tax, $total)";
							if ( !mysqli_query($con, $sql) ) 
							{
								die( 'Error: ' . mysqli_error($con) );
							}
							
					 foreach($_SESSION['orders'] as $anOrder) //insert info for each sammich 
                    {
							//storing all object data in individual variables to allow for easier SQL querying
							$anOrder->translateToEnglish();
							$sandwichType = $anOrder->sandwichType;
							$bread = $anOrder->bread;
							$meat = $anOrder->meat;
							$cheese = $anOrder->cheese;
							$veggies = $anOrder->veggies;
							$condiments = $anOrder->condiments;
							$drink = $anOrder->drink;
							$chips = $anOrder->chips;
							$dessert = $anOrder->dessert;
							$total = $anOrder->total;
							$randomOrderId = rand(1,9999);
							$sandwichType = str_replace("'", "", $sandwichType);//sql doesn't like the single-quotes in strings
							$sql = "INSERT INTO orders (`order_id`, `sandwichType`, `bread`, `meat`, `cheese`, `veggies`, 
							`condiments`, `drinks`, `chips`, `desserts`) VALUES ($randomOrderId, '$sandwichType', '$bread', '$meat', '$cheese', '$veggies',
							'$condiments', '$drink', '$chips', '$dessert')";
							if ( !mysqli_query($con, $sql) ) 
							{
								die( 'Error: ' . mysqli_error($con) );
							}
					}
                    header( "refresh:1;url=esp_completeOrder.php" );//go to order complete page
                }
                else
                    echo "<h5 style = \"color:red\">Advertencia! La cantidad ingresada no es suficiente. Esta es su ultima advertencia.</h5>";
            }
                    //if so, check that number entered is greater than amount due
                    //if so, go to "Your Order Has Been Submitted" "Printing Receipt" "Please collect your change".
            
        ?>
              
            <form method = "POST" action = "">
                <input type = "submit" name = "card" value = "Pagar con Tarjeta de Credito">
        <?php
            if( isset($_POST['card']) )
            {
                $cardNumber = NULL;
                $randDigits = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
                for($i=0; $i<10; $i++)
                {
                    $cardNumber = $cardNumber . $randDigits[array_rand($randDigits)];
                }
                $_SESSION['cardNum'] = $cardNumber;
                echo "card-swipe: " . "[" . $cardNumber . "]";
				
				/**SQL for putting sandwiches and stuff in the order**/
				//storing all object data in individual variables to allow for easier SQL querying
			
				$subtotal = $_SESSION['orderTotal'];
				$tax = floor(($subtotal*(.08))*100)/100;
				$total = $tax + $subtotal;
				$randomOrderId = rand(1,9999);
				//update sales table with data about sandwiches
				$sql = "INSERT INTO sales (`order_id`, `subtotal`, `tax`, `total`) VALUES ($randomOrderId, $subtotal, $tax, $total)";
				if ( !mysqli_query($con, $sql) ) 
				{
					die( 'Error: ' . mysqli_error($con) );
				}
				
				foreach ($_SESSION['orders'] as $anOrder) {
					$anOrder->translateToEnglish();
					$sandwichType = $anOrder->sandwichType;
					$bread = $anOrder->bread;
					$meat = $anOrder->meat;
					$cheese = $anOrder->cheese;
					$veggies = $anOrder->veggies;
					$condiments = $anOrder->condiments;
					$drink = $anOrder->drink;
					$chips = $anOrder->chips;
					$dessert = $anOrder->dessert;
					$total = $anOrder->total;
					$sandwichType = str_replace("'", "", $sandwichType);
					$randomOrderId = rand(1,9999);			
					}
				$sql = "INSERT INTO orders (`order_id`, `sandwichType`, `bread`, `meat`, `cheese`, `veggies`, 
						`condiments`, `drinks`, `chips`, `desserts`) VALUES ($randomOrderId, '$sandwichType', '$bread', '$meat', '$cheese', '$veggies',
						'$condiments', '$drink', '$chips', '$dessert')";
				if ( !mysqli_query($con, $sql) ) 
				{
					die( 'Error: ' . mysqli_error($con) );//if sql fails kill customer
				}
                header( "refresh:1;url=esp_completeOrder.php" );
            }
           
        ?>
            </form>
    </div>

</body>
</html>